mui.init({
	swipeBack: true
})
mui.plusReady(function() {
	var cityCheck = ''
	var self = plus.webview.currentWebview();
	if(self.parameter != '' && self.parameter) {
		$(".mui-title").text('修改地址')
		console.log(JSON.stringify(self.parameter))
		$("#userName").val(self.parameter.receiver)
		$("#phone").val(self.parameter.mobile)
		$('.selectaddress').text(self.parameter.province + ' ' + self.parameter.city + ' ' + self.parameter.area)
		cityCheck = [{
			value: self.parameter.provinceId
		}, {
			value: self.parameter.cityId
		}, {
			value: self.parameter.areaId
		}]
		$(".address").val(self.parameter.subAdds)
	}

	var cityPicker3 = new mui.PopPicker({
		layer: 3
	});

	cityPicker3.setData(cityData);
	var showCityPickerButton = document.getElementById('showCityPicker3');
	showCityPickerButton.addEventListener('tap', function(event) {
		if(self.parameter != '' && self.parameter) {
			cityPicker3.pickers[0].setSelectedValue(self.parameter.provinceId, 0, function() {
				setTimeout(function() {
					cityPicker3.pickers[1].setSelectedValue(self.parameter.cityId, 0, function() {
						setTimeout(function() {
							cityPicker3.pickers[2].setSelectedValue(self.parameter.areaId)
						})
					});
				}, 100);
			});
		}

		cityPicker3.show(function(items) {
			cityCheck = items
			console.log(JSON.stringify(items))
			$('.selectaddress').text(items[0].text + ' ' + items[1].text + ' ' + items[2].text)

			//					cityResult3.innerText = "你选择的城市是:" + _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
			//返回 false 可以阻止选择框的关闭
			//return false;
		});
	}, false);

	$('.footer').on("tap", function() {
		if(common.replace($('#userName').val()) == '') {
			common.toast('收件人名字不能为空')
			return
		} else if(!common.phone.test($('#phone').val())) {
			common.toast('请输入正确的手机号码')
			return
		} else if($(".selectaddress") == '' || cityCheck == '') {
			common.toast('收货地址不能为空')
			return
		} else if($(".address").val() == '') {
			common.toast('请输入详细地址')
			return
		}

		var data = {
			receiver: $('#userName').val(),
			mobile: $('#phone').val(),
			provinceId: cityCheck[0].value,
			cityId: cityCheck[1].value,
			areaId: cityCheck[2].value,
			subAdds: $(".address").val()
		}
		if(self.parameter.addId) {
			data.addrId = self.parameter.addId
		}
		common.jsonAjax('/userAddress/saveAddress', data, function(data) {
			if(data.code == 1) {
				plus.webview.getWebviewById('address.html').reload()
				mui.back()
				if(self.parameter.isDefault == 1) {
					var list = plus.webview.getWebviewById("../my/userinfo.html")
					mui.fire(list, 'refurbish');
				}
				var goBuy = plus.webview.getWebviewById('../order/confirmorder.html');
				mui.fire(goBuy, 'goBuy');
			} else {
				if(data.resultMsg) {
					common.toast(data.resultMsg)
				} else {
					console.log(data)
				}
			}
		})
	})

})